package com.ssyx.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssyx.common.auth.AuthContextHolder;
import com.ssyx.common.result.Result;
import com.ssyx.model.order.OrderInfo;
import com.ssyx.order.service.OrderInfoService;
import com.ssyx.vo.order.OrderConfirmVo;
import com.ssyx.vo.order.OrderSubmitVo;
import com.ssyx.vo.order.OrderUserQueryVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName:OrderApiController
 * Description:
 *
 * @Author kay
 * @Create 2024/5/22 9:15
 * @Version 1.0
 */
@RestController
@RequestMapping(value="/api/order")
public class OrderApiController {

    @Autowired
    private OrderInfoService orderService;

    //确认订单
    @GetMapping("auth/confirmOrder")
    public Result confirm() {
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        return Result.ok(orderConfirmVo);
    }

    //生成订单
    @PostMapping("auth/submitOrder")
    public Result submitOrder(@RequestBody OrderSubmitVo orderSubmitVo) {
        Long orderId = orderService.submitOrder(orderSubmitVo);
        return Result.ok(orderId);
    }

    //获取订单详情
    @GetMapping("auth/getOrderInfoById/{orderId}")
    public Result getOrderInfoById(@PathVariable("orderId") Long orderId){
        OrderInfo orderInfo = orderService.getOrderInfoById(orderId);
        return Result.ok(orderInfo);
    }

    //获取用户订单分页列表
    @GetMapping("auth/findUserOrderPage/{page}/{limit}")
    public Result findUserOrderPage(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    OrderUserQueryVo orderUserQueryVo){
        Long userId = AuthContextHolder.getUserLoginVo().getUserId();
        orderUserQueryVo.setUserId(userId);
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        IPage<OrderInfo> pageModel = orderService.findUserOrderPage(pageParam, orderUserQueryVo);
        return Result.ok(pageModel);
    }
}
